package schedule

import (
	"fmt"
	"gin_cli/models"
)

type Wehcater struct {
	Openid     string
	AccountId  int64
	MerchantId int64
	StoreIds   []int64
}

func SendReportForm() {
	defer cronErr()
	db := models.Db
	fmt.Println("CORN  定时任务 SendTpl")
	RelAccountWechat := &models.RelAccountWechat{}
	RelAccountWechatList := RelAccountWechat.GetList(make(map[string]interface{}))
	Wehcaters := make([]Wehcater, len(RelAccountWechatList))
	accountIds := make([]int64, len(RelAccountWechatList))
	for i := 0; i < len(RelAccountWechatList); i++ {
		accountIds[i] = RelAccountWechatList[i].AccountId
		Wehcaters[i].AccountId = RelAccountWechatList[i].AccountId
		Wehcaters[i].Openid = RelAccountWechatList[i].Openid
	}

	AccountRelationList := make([]models.AccountRelation, len(accountIds))
	db.
		Where(
			"account_id in (?) AND type = 1 ",
			accountIds,
		).
		Find(&AccountRelationList)

	relationIds := make([]int64, len(AccountRelationList))
	for i := 0; i < len(AccountRelationList); i++ {
		relationIds[i] = AccountRelationList[i].RelationId
		for j := 0; j < len(Wehcaters); j++ {
			if Wehcaters[j].AccountId == AccountRelationList[i].AccountId {
				Wehcaters[j].MerchantId = AccountRelationList[i].RelationId
			}
		}
	}
	merchatnStores := []models.MerchantStore{}
	db.
		Where(
			"merchant_id in (?)",
			relationIds,
		).
		Find(&merchatnStores)

	for i := 0; i < len(Wehcaters); i++ {
		ids := []int64{}
		for j := 0; j < len(merchatnStores); j++ {
			if Wehcaters[i].MerchantId == merchatnStores[j].MerchantId {
				ids = append(ids, merchatnStores[j].StoreId)
			}
		}
		Wehcaters[i].StoreIds = ids
	}
	fmt.Println(Wehcaters)
	fmt.Println("CORN  定时任务 SendTpl   END")
}
